package com.example.threaddemo.util;

import java.io.IOException;
import java.net.*;

/**
 * @Author Boolean
 * @Date 2021/3/23 11:29
 * @Version 1.0
 */
public class JkTest {

    public static void main(String[] args) {
        System.out.println(isHostConnectable("10.1.27.111",135,8080));
        System.out.println(isHostReachable("10.1.27.111",8080));

    }



    public static boolean isHostConnectable(String host, int port, Integer timeOut) {
        Socket socket = new Socket();
        try {
            socket.connect(new InetSocketAddress(host, port));
          //  SocketAddress socketAddress = new InetSocketAddress(host, port);
            //socket.connect(socketAddress, timeOut);
           // socket.close();

        } catch (IOException e) {
            //e.printStackTrace();
            return false;
        } finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    public static boolean isHostReachable(String host, Integer timeOut) {
        try {
            return InetAddress.getByName(host).isReachable(timeOut);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

}
